package com.cityguardian.cityguardianapp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.cityguardian.model.Usuario;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class RegisterActivity extends ActionBarActivity {
	public final static String EXTRA_MESSAGE = "com.cityguardian.cityguardian.registerActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		// Show the Up button in the action bar.
		setupActionBar();
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(false);
		}
		else {
		    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.register, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				NavUtils.navigateUpFromSameTask(this);
				return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void nextRegister(View view){
    	Intent intent = new Intent(this, Register2Activity.class);
    	TextView userName = (TextView) findViewById(R.id.editText1);
    	TextView password = (TextView) findViewById(R.id.editText2);
    	TextView nombres = (TextView) findViewById(R.id.editText3);
    	TextView apellidos = (TextView) findViewById(R.id.editText4);
    	TextView fechaNac = (TextView) findViewById(R.id.editText5);    	
    	
    	String usrNam = userName.getText().toString();
    	String pass = password.getText().toString();
    	String names = nombres.getText().toString();
    	String apell = apellidos.getText().toString();
    	String fecha = fechaNac.getText().toString();
    	
    	Usuario usuario = new Usuario();
    	usuario.setUsuario(usrNam);
    	usuario.setPassword(pass);
    	usuario.setNombres(names);
    	usuario.setApellidos(apell);
    	SimpleDateFormat fmt=new SimpleDateFormat("dd/MM/yyyy");
    	String dateString = "";
    	try {
			Date inputDate = fmt.parse(fecha);
			fmt = new SimpleDateFormat("yyyy-MM-dd");
			dateString = fmt.format(inputDate);			
		} catch (ParseException e){
			e.printStackTrace();
		}
    	
    	usuario.setFechaNacimiento(dateString);
    	
    	intent.putExtra(EXTRA_MESSAGE, usuario);
    	startActivity(intent);
	}

}
